//
//  ReportDepView.m
//  Bus
//
//  Created by wangjiajia on 13-8-14.
//  Copyright (c) 2013年 wangjiajia. All rights reserved.
//

#import "ReportDepView.h"
#import "MBProgressHUD.h"
#import "Tools.h"
#import "WebServicesUtils.h"
#import "JsonParser.h"
#import "ASIHTTPRequest.h"
#import "RepDepInfo.h"
#import "InfoDetails.h"

//2、工作汇报部门列表
@interface ReportDepView (){
    MBProgressHUD* hud;
    ASIHTTPRequest* globalRequest;
}

@end

@implementation ReportDepView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //添加返回按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    
    [leftButton setBackgroundImage:[UIImage imageNamed:@"brown_titlebar_back.png"] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"brown_titlebar_back_selected.png"] forState:UIControlStateHighlighted];
    [leftButton setTitle:@" 返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font=[UIFont fontWithName:@"-" size:18];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = back;
    
    self.navigationItem.title=[NSString stringWithFormat:@"%@ 汇报部门",self.repTime];
    
    self.repDepArray=[[NSMutableArray alloc] initWithCapacity:12];
    
    if(!hud)
        hud=[[MBProgressHUD alloc] initWithView:self.view];
    [Tools showHUD:@"正在获取数据..." andView:self.view andHUD:hud];
    globalRequest=[WebServicesUtils getRepDepList:self.repTime withDelegate:self];

}


-(void)viewWillDisappear:(BOOL)animated{
    //如果立即返回，请求没有结束，会释放一些nil对象照成Bad_Access?
    ////NSLog(@"viewWillDisappear");//被调用
 
        if (globalRequest) {
            [globalRequest clearDelegatesAndCancel];
        }
        [super viewWillDisappear:animated];
    
    
}
-(void)viewWillUnload{
    //NSLog(@"viewDidUnload");//没调用
    [super viewWillUnload];
}

- (void)viewDidUnload {
    //NSLog(@"viewDidUnload");//没调用
    [self setRepDepTableView:nil];
    [super viewDidUnload];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate协议实现

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //颜色会自动缓存，不会在下次重绘时丢失。
   UITableViewCell* selectedCell= [tableView  cellForRowAtIndexPath:indexPath];
    selectedCell.textLabel.textColor=[UIColor redColor];
    
    InfoDetails* details=[[InfoDetails alloc] init];
    details.newsID=((RepDepInfo*)[self.repDepArray objectAtIndex:indexPath.row]).RepID;
    details.isDepReport=YES;
    details.navigationTitle=@"汇报内容";
    
    [self.navigationController pushViewController:details animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return 44;
}
#pragma mark - UITableViewDataSource协议实现

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.repDepArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static  NSString * identifier=@"RepDepCellIdentifier";
    UITableViewCell *cell=[self.repDepTableView dequeueReusableCellWithIdentifier:identifier];
    if(cell==nil){
        //不变的放这里。自带的或者自定义的TableViewCell都行。
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.backgroundColor=[UIColor clearColor];
               cell.backgroundView=[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"news_bg@2x.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];
        cell.textLabel.backgroundColor=[UIColor clearColor];
        
    }
    
    //变化的放这里:绑定值
    
    cell.textLabel.lineBreakMode=UILineBreakModeMiddleTruncation;
    NSString* DepName=((RepDepInfo*)[self.repDepArray objectAtIndex:indexPath.row]).RepDepName ;
    //去掉两边空格
    NSString* trimDepName=[DepName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    cell.textLabel.text=[NSString stringWithFormat:@"%@工作汇报", trimDepName];
    
    return cell;
    
}


#pragma mark- 网络处理

-(void)requestFinished:(ASIHTTPRequest *)request{
    if(hud){
        [hud hide:YES];
    }
    NSString* responseStr=[request responseString];
    self.repDepArray=[JsonParser parseAsRepDepList:responseStr];
    [self.repDepTableView reloadData];
        
}

-(void)requestFailed:(ASIHTTPRequest *)request{
    
    if(hud){
        [hud hide:YES];
    }
    
    [Tools toastNotification:@"处理请求失败，请检查网络连接!" andView:self.view andLoading:NO andIsBottom:NO];
}


#pragma mark- 返回到上一个视图
-(void)backTo{
    [self.navigationController popViewControllerAnimated:YES];
}

@end